package com.wokeMy.view.channe.huicao;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.JavascriptInterface;
import android.webkit.WebSettings;
import android.webkit.WebSettings.LayoutAlgorithm;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

import com.example.woke.MainActivity;
import com.wokeMy.view.util.Util;
import com.zhongcai.online.R;

public class HuicHtmlActivity extends Activity {

    private String url, urlname;
    private WebView mWebView = null;
    private Intent intent;
    //	public static String TEST_IMAGE;
    //	private static final String FILE_NAME = "/ic_icon2.jpg";
    private ClipData myClip;
    private ClipboardManager myClipboard;

    private Handler handler;
    private Dialog dialog;
    String htmll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_yin_zct_html);
        dialog= Util.createLoadingDialog(HuicHtmlActivity.this);
        intent = getIntent();
        intvkew();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        //判断是否可以返回操作
        if (mWebView.canGoBack() && event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            mWebView.goBack();

        } else {
            mWebView.destroy();
            finish();
        }
        return true;

    }

    @SuppressLint("JavascriptInterface")
    private void intvkew() {

        findViewById(R.id.back_yinzct_iv).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(HuicHtmlActivity.this,
                        MainActivity.class);
                intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(intent);
                finish();
            }
        });

        myClipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);

        url = intent.getStringExtra("html");
//        url ="http://88china.6655.la/index.php/api/zhongfu/secpay/order_no/84ZF820732017123034713";
//        urlname = intent.getStringExtra("urlname");
        Log.e("urlname", "$" + url);
//        TextView mTitel = (TextView) findViewById(R.id.avhtml_text_title);
//        mTitel.setText(urlname);
        mWebView = (WebView) findViewById(R.id.webView1_yinzct_wv);
        WebSettings settings = mWebView.getSettings();
        setSettings(settings);
        mWebView.addJavascriptInterface(new InJavaScriptLocalObj(), "java_obj");
        mWebView.setWebViewClient(new MyWebViewClient());
        //		mWebView.loadUrl("http://www.88china.com");
        if (!TextUtils.isEmpty(url)) {
            if (url.startsWith("http")) {
                mWebView.loadUrl(url);
            } else {
                mWebView.loadUrl("http://" + url);
            }
        }
    }
    private void setSettings(WebSettings settings) {
        settings.setJavaScriptEnabled(true);// 设置可以运行JS脚本
        settings.setDefaultTextEncodingName("UTF-8");
        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setLayoutAlgorithm(LayoutAlgorithm.SINGLE_COLUMN);
        settings.setLoadWithOverviewMode(true);
        mWebView.setBackgroundColor(getResources().getColor(R.color.white)); // 设置背景色
        //		mWebView.getBackground().setAlpha(50); // 设置填充透明度 范围：0-255
        //		settings.setSupportZoom(false);// 用于设置webview放大
        //		settings.setBuiltInZoomControls(false);
        settings.setDomStorageEnabled(true);
        //		settings.setTextSize (TextSize.NORMAL);
        ////		settings.setPluginState(PluginState.ON);
        settings.setDatabaseEnabled(true);
    }


    // 监听
    private class MyWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (url != null) {
                Log.e("UrlLoading",url);
//                Uri uri = Uri.parse(url);
//                Intent intent = new Intent(Intent.ACTION_VIEW, uri);
//                startActivity(intent);
                view.loadUrl(url);
            }
            return false;

        }
        @Override
        public void onPageFinished(WebView view, String url) {
            view.getSettings().setJavaScriptEnabled(true);
            mWebView.setVisibility(View.VISIBLE);
            Util.closeDialog(dialog);
            view.loadUrl("javascript:window.java_obj.getSource('<head>'+" +
                    "document.getElementsByTagName('html')[0].innerHTML+'</head>');");
//            Log.e("onPageFinishedurl......",url);
//            Log.e("onPageFinishedgetUrl",view.getUrl());
//            Log.e("getOriginalUrl()",view.getOriginalUrl());
            super.onPageFinished(view, url);
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            view.getSettings().setJavaScriptEnabled(true);
            Log.e("onPageStarted",url);

            super.onPageStarted(view, url, favicon);
        }

        @Override
        public void onReceivedError(WebView view, int errorCode,
                                    String description, String failingUrl) {
            super.onReceivedError(null, 1, "访问失败", null);
            Log.e("onReceivedError......",failingUrl);
        }
    }

    /**
     * 逻辑处理
     * @author zhengpeng
     */
    final class InJavaScriptLocalObj {
        @JavascriptInterface
        public void getSource(String html) {
            Log.d("html=", html);
            htmll=html;
            if(html.contains("付款成功")){
                handler = new Handler();
                // 延迟SPLASH_DISPLAY_LENGHT时间然后跳转到MainActivity
                handler.postDelayed(new Runnable() {

                    @Override
                    public void run() {
                        Intent intent = new Intent(HuicHtmlActivity.this,
                                MainActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        startActivity(intent);
                        Toast.makeText(HuicHtmlActivity.this,"支付成功",Toast.LENGTH_SHORT).show();
                        finish();
                    }
                }, 1000);
            }
        }
    }


}
